/*
 Copyright (c) 2010, Advanced Micro Devices
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification, are permitted 
 provided that the following conditions are met:
 
 Redistributions of source code must retain the above copyright notice, this list of conditions 
 and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright notice, this list of 
 conditions and the following disclaimer in the documentation and/or other materials provided 
 with the distribution.
 
 Neither the name of Advanced Micro Devices nor the names of its contributors may be used to endorse 
 or promote products derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 POSSIBILITY OF SUCH DAMAGE.
 */

#include <QtGui>
#include "Kernel.h"
#include "KernelPropertiesDialog.h"
#include "EditorWindow.h"

#include <sstream>

Kernel::Kernel( EditorView * view, EditorWindow * window ) 
: Node::Node( view, window )
{
    myTextColor = Qt::darkGreen;
    myOutlineColor = Qt::darkBlue;
    myBackgroundColor = Qt::white;

	numParametersIs( 0 );
	
    setFlags(ItemIsMovable | ItemIsSelectable);
}

Kernel::~Kernel()
{
    foreach (Link *link, myLinks)
	delete link;
	window()->kernels().remove( name() );
}


QRectF Kernel::outlineRect() const
{
    const int Padding = 8;
    QFontMetricsF metrics = qApp->font();
    QRectF rect = metrics.boundingRect(myText);
    rect.adjust(-Padding, -Padding, +Padding, +Padding);
    rect.translate(-rect.center());
    return rect;	
}


QPainterPath Kernel::shape() const
{
    QRectF rect = outlineRect();
	
    QPainterPath path;
#if 1
    path.addRoundRect(rect, roundness(rect.width()),
                      roundness(rect.height()));
#else
	path.addEllipse( rect );
#endif
    return path;
}

void Kernel::formatText()
{
	setText( name());
}

void Kernel::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
	propertiesDialog();
}

void Kernel::propertiesDialog()
{
	KernelPropertiesDialog dialog( this, view() );
	dialog.exec();
}


void Kernel::writeXml( QXmlStreamWriter & xmlsw ) const
{
	xmlsw.writeStartElement( "kernel" );
	Node::writeXml( xmlsw );
	std::stringstream o;
	o << numParameters();
	std::string s = o.str();
	xmlsw.writeTextElement( "numParameters", QString( s.c_str() ));
	for( int i = 1; i <= numParameters(); ++i ){
		xmlsw.writeStartElement( "parameter" );
		xmlsw.writeTextElement( "storageClass", mStorageClass[ i ] );
		xmlsw.writeTextElement( "dataType", mDataType[ i ] );
		xmlsw.writeTextElement( "parameterName", mParameter[ i ] );
		xmlsw.writeTextElement( "ioClass", mIoClass[ i ] );
		xmlsw.writeTextElement( "volatile", checkStateString( mVolatile[ i ] ));
		xmlsw.writeEndElement();
	}//for
	xmlsw.writeEndElement();
}

void Kernel::readXml( QXmlStreamReader & xmlsr ) 
{
	int idx = 0;
	while( !xmlsr.atEnd() ){
		QXmlStreamReader::TokenType tokenType = xmlsr.readNext();
		if( tokenType == QXmlStreamReader::StartElement ){
			if( xmlsr.name() == "node" ){
				Node::readXml( xmlsr );
			}
			else if( xmlsr.name() == "numParameters" ){
				QString numPString = xmlsr.readElementText();
				int numP = numPString.toShort();
				numParametersIs( numP );
			}
			else if( xmlsr.name() == "parameter" ){
				idx++;
			}
			else if( xmlsr.name() == "storageClass" ){
				mStorageClass[ idx ] = xmlsr.readElementText();
			}
			else if( xmlsr.name() == "dataType" ){
				mDataType[ idx ] = xmlsr.readElementText();
			}
			else if( xmlsr.name() == "parameterName" ){
				mParameter[ idx ] = xmlsr.readElementText();
			}
			else if( xmlsr.name() == "ioClass" ){
				mIoClass[ idx ] = xmlsr.readElementText();
			}
			else if( xmlsr.name() == "volatile" ){
				mVolatile[ idx ] = stringCheckState( xmlsr.readElementText() );
			}
		}
		else if( tokenType == QXmlStreamReader::EndElement ){
			if( xmlsr.name() == "kernel" ){
				break;
			}
		}
	}// while
}

void Kernel::build( std::ofstream & file )
{
	file << "Kernel " << name().toStdString() << ":( " << std::endl;
	// new kernel signature
	bool first = true;
	for( int i = 1; i <= numParameters(); ++i ){
		if( !first ){
			file << ", " << std::endl;
		}
		first = false;
		file << mStorageClass[ i ].toStdString() << " " 
			<< mDataType[ i ].toStdString() << " "
			<< mParameter[ i ].toStdString() << " "
			<< mIoClass[ i ].toStdString();
	}//for
	file << std::endl << "){}" << std::endl;
}
